package com.jic.product.controller.store;
import com.jic.common.base.vo.PageResult;
import com.jic.product.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.jic.common.base.vo.RestResult;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.product.model.vo.request.MStoreVirtualRequest;
import com.jic.product.model.vo.response.MStoreVirtualResponse;
import com.jic.product.model.entity.MStoreVirtual;
import com.jic.product.service.MStoreVirtualService;
/**
 * @Author: 建投数据科技(山东)有限公司 
 * @date: 2020-04-24 20:13:58
 */
 @RestController
 @Slf4j
 @RequestMapping("/mStoreVirtual")
 @Api(tags = " [虚拟仓库表]控制类-PC")
public class MStoreVirtualController {
	@Autowired
	private MStoreVirtualService service;
	
	/**delete
	 * 新增记录
	 * @param request 新增[虚拟仓库表]请求参数
	 * @return MStoreVirtualResponse 新增[虚拟仓库表]响应参数
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "新增[虚拟仓库表]记录")
	@PostMapping("/save")
	@ResponseBody
	public RestResult<Long> save(@RequestBody MStoreVirtualRequest request) throws Throwable {

		long flag = service.save(request);
	 return RestResult.success(flag);
	}
	/**
	 * 新增或修改记录
	 * @param request 新增或修改[虚拟仓库表]请求参数
	 * @return MStoreVirtualResponse 新增或修改[虚拟仓库表]响应参数
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "保存或更新[虚拟仓库表]")
	@PostMapping("/saveOrUpdate")
	 @ResponseBody
	public RestResult<Long> saveOrUpdate(@Validated @RequestBody MStoreVirtualRequest request) throws Throwable {

		long flag =   service.saveOrUpdate(request);
	 	return RestResult.success(flag);
	}
	/**
	 * 删除记录
	 * @param request 删除[虚拟仓库表]请求参数
	 * @return MStoreVirtualResponse 删除[虚拟仓库表]响应参数
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "删除[虚拟仓库表]记录")
	@PostMapping("/delete")
	@ResponseBody
	public RestResult<Long> delete(@RequestBody MStoreVirtualRequest request) throws Throwable {
			MStoreVirtual mStoreVirtual = new MStoreVirtual();
			BeanUtils.copyProperties(request, mStoreVirtual);
			mStoreVirtual.setDeptId(request.getAdminOperate().getDeptId());
		long flag = service.deleteByPrimaryKeyLogically(mStoreVirtual);
	 return RestResult.success(flag);
	}
	/**
	 * 修改记录
	 * @param request 修改[虚拟仓库表]请求参数
	 * @return MStoreVirtualResponse 修改[虚拟仓库表]响应参数
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-3-11 20:54:04
	 */
	@ApiOperation(value = "修改[虚拟仓库表]记录")
	@PostMapping("/update")
	@ResponseBody
	public RestResult<Long> update(@RequestBody MStoreVirtualRequest request) throws Throwable {
			MStoreVirtual mStoreVirtual = new MStoreVirtual();
			BeanUtils.copyProperties(request, mStoreVirtual);
		mStoreVirtual.setDeptId(request.getAdminOperate().getDeptId());
		long flag = service.updateByPrimaryKeySelective(mStoreVirtual);
	 return RestResult.success(flag);
	}

	/**
	 * 批量修改虚拟仓状态
	 * @param request
	 * @return
	 * @throws Throwable
	 */

	@ApiOperation(value = "修改[实体仓库表]记录状态")
	@PostMapping("/updateStatus")
	@ResponseBody
	public RestResult<Long> updateStatus(@RequestBody MStoreVirtualRequest request) throws Throwable {
		long flag = 0;
		Long[] ids = request.getIds();
		for (Long id : ids) {
			MStoreVirtual mStore = new MStoreVirtual();
			mStore.setId(id);
			mStore.setStatus(request.getStatus());
			mStore.setDeptId(request.getAdminOperate().getDeptId());
			flag = service.updateByPrimaryKeySelective(mStore);
		}
		return RestResult.success(flag);
	}
	/**
	 * 单表查询详情信息
	 * @param request 查询[虚拟仓库表]详情请求参数
	 * @return MStoreVirtualResponse 查询[虚拟仓库表]详情响应参数
	 * author 建投数据科技(山东)有限公司
	 * @date 2020-3-11 20:54:04
	 */
	 @PostMapping("/getByPrimaryKey")
	 @ApiOperation(value = "查询[虚拟仓库表]详情")
	 @ResponseBody
	public RestResult<MStoreVirtualResponse> getByPrimaryKey(@Validated  @RequestBody MStoreVirtualRequest request) {
		log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
		if (request!=null && request.getId()!=null) {

			MStoreVirtual mStoreVirtual = new MStoreVirtual();
			BeanUtils.copyProperties(request, mStoreVirtual);
			MStoreVirtual result = service.getByPrimaryKey(mStoreVirtual);
			log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
			if (result == null) {
				return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
			}
			MStoreVirtualResponse bo = new MStoreVirtualResponse();
			BeanUtils.copyProperties(result, bo);
			return RestResult.success(bo);
		}
		log.info("查询详情请求结束");
		return RestResult.success(null);
	}
	 /**
		 * 分页查询
		*
		* @param request 分页查询请求参数
		* @return 分页列表
		  * @throws Throwable 业务异常和系统异常
		 */
	@PostMapping("/listPage")
	@ApiOperation(value = " 分页查询列表 ")
	@ResponseBody
	public RestResult<PageResult<MStoreVirtualResponse>> listPage (@RequestBody MStoreVirtualRequest request) throws Throwable {
	// MStoreVirtual param = new MStoreVirtual();
	// BeanUtils.copyProperties(request, param);
	return RestResult.success(service.listPageBySelective(request,request.getPageNum(),  request.getPageSize()));
	}

	/**
		 * M_STORE_VIRTUAL 查询列表（不分页）
		 * @param  request 前端请求参数
	 */
	 @PostMapping("/listAll")
	 @ApiOperation(value = "不分页查询 [虚拟仓库表] ")
	 @ResponseBody
	 public RestResult<List<MStoreVirtualResponse>>  listMStoreVirtualAllRecord(@RequestBody MStoreVirtualRequest request) throws Throwable {
			MStoreVirtual mStoreVirtual = new MStoreVirtual();
			BeanUtils.copyProperties(request, mStoreVirtual);
			List<MStoreVirtual> list = service.listAllRecord(mStoreVirtual);
				List<MStoreVirtualResponse> responseList = list.stream().map(temp -> {
				MStoreVirtualResponse bo = new MStoreVirtualResponse();
				BeanUtils.copyProperties(temp, bo);
				return bo;
				}).filter(item -> item != null).collect(Collectors.toList());
			return RestResult.success(responseList);
	}

}

